<template>
  <div :id="id" :class="className" class="pie-chart" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'pie-chart'
    },
    id: {
      type: String,
      default: 'pieChart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    }
  },
  data () {
    return {
      chart: null
    }
  },
  mounted () {
    // this.initChart()
  },
  beforeDestroy () {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart (initDatas) {
      /**
       * initDatas 为 Object 格式
       * @params title String 图标的标题
       * @params legendData Array[String] 图标图例数据
       * @params seriesData Array[Object] 图标系列数据
       */
      this.chart = echarts.init(document.getElementById(this.id))
      this.chart.setOption({
        title: {
          text: initDatas.name,
          subtext: null,
          x: 'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b}: {c} ({d}%)'
        },
        legend: {
          orient: 'vertical',
          left: 'left',
          data: initDatas.legendData
        },
        series: [
          {
            name: initDatas.name,
            type: 'pie',
            radius: '50%',
            center: ['50%', '60%'],
            itemStyle: {
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            },
            data: initDatas.seriesData
          }
        ]
      })
    }
  }
}
</script>
